package com.gxa.controller;


import com.gxa.pojo.Article;
import com.gxa.service.ArticleService;
import com.gxa.service.impl.ArticleServiceImpl;
import com.gxa.utils.ResponseData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author 42818
 */
@RestController
@Api(tags = "文章管理控制器")
@RequestMapping("/article")
public class ArticlrController {

    @Autowired
    private ArticleServiceImpl articleService;


    @PostMapping("/addArticle")
    @ApiOperation("添加文章")
    @ResponseBody
    public ResponseData addArticle(@ApiParam(value = "文章实体参数",required = true) @RequestBody Article article){

        if(articleService.addArticle(article)){
            return new ResponseData(1,"添加成功");
        }else {
            return new ResponseData(0,"添加失败");
        }
    }

    @GetMapping("/showArticlesByPage")
    @ApiOperation("显示所有文章")
    @ResponseBody
    public ResponseData showArticlesByPage(@ApiParam(value = "显示第几页",required = true) int pageNum,@ApiParam(value = "每页显示几条",required = true) int pageSize){

        if(articleService.showArticlesByPage(pageNum,pageSize)!=null){
            return new ResponseData(1,"显示成功",articleService.showArticlesByPage(pageNum,pageSize));
        }else {
            return new ResponseData(0,"显示失败");
        }
    }

    @ApiOperation("根据文章标题删除文章")
    @DeleteMapping("/deleteArticle")
    @ResponseBody
    public ResponseData deleteArticleByTitle(@ApiParam(value = "要删除的文章标题",required = true) String title){
        if(articleService.deleteArticleByTitle(title)){
            return new ResponseData(1,"删除成功");
        }else {
            return new ResponseData(0,"删除失败");
        }
    }

    @ApiOperation("根据文章标题或类型查询文章")
    @GetMapping("/selectArticle")
    @ResponseBody
    public ResponseData selectArticleByTitleOrType(@ApiParam(value = "要查询文章的标题",required = false) String title,@ApiParam(value = "要查询文章的类型",required = false) String type){
        if(articleService.selectArticleByTitleOrType(title,type)!=null){
            return new ResponseData(1,"查询成功",articleService.selectArticleByTitleOrType(title, type));
        }else {
            return new ResponseData(0,"查询失败");
        }
    }


    @ApiOperation("根据文章标题批量删除文章")
    @DeleteMapping("/batchDelete")
    public ResponseData batchDelete(@ApiParam(value = "要删除多个的文章标题字符串，用‘，’隔开",required = true) String titles){

        String[] strings=titles.split(",");
//
//        for (String title : strings) {
//            System.out.println(title);
//        }

        if(articleService.batchDelete(strings)){
            return new ResponseData(1,"删除成功");
        }else {
            return new ResponseData(0,"删除失败");
        }
    }

}
